//
//  MyAdvisoryOrderFinishController.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/4/11.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

class MyAdvisoryOrderFinishController: BaseViewController, YSGNurseAppointMentStarRatingBGViewDelegate {
    
    var diagnoseId: String
    
    var closure: (() -> Void)?

    var viewModel: MyAdvisoryOrderFinishViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    init(with diagnoseId: String) {
        self.diagnoseId = diagnoseId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        setUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNav(type: 0)
        setBack()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        setNav(type: 1)
    }
    
    func setBack() {
        let backItem = UIBarButtonItem(imageName: "back_arrow_white", target: self, action: #selector(back))
        if UIDevice.avaiableSystem < 11 {
            let spaceItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            spaceItem.width = -10.0
            navigationItem.leftBarButtonItems = [spaceItem, backItem]
        } else {
            navigationItem.leftBarButtonItem = backItem
        }
    }
    
    func setNav(type: Int) {
//        // 0:appear 1:disappear
//        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.medium(18),
//                                                                   NSAttributedString.Key.foregroundColor: type == 0 ? UIColor.white : UIColor.textDark]
//
//        if type == 0 {
//
//            setNavClear()
//
//        }  else {
//           setNavWhite()
//        }
    }
    
    @objc func back() {
        navigationController?.popViewController(animated: true)
    }
    
    func setUI() {
        self.title = "详情"
        tableView.tableFooterView = UIView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.register(MyAdvisoryOrderFinishHeaderCell.nibForCell(),
                           forCellReuseIdentifier: MyAdvisoryOrderFinishHeaderCell.reuseIdentifier())
        tableView.register(MyAdvisoryOrderFinishDiagnoseCell.nibForCell(),
                           forCellReuseIdentifier: MyAdvisoryOrderFinishDiagnoseCell.reuseIdentifier())
        tableView.register(MyAdvisoryOrderFinishEntranceCell.nibForCell(),
                           forCellReuseIdentifier: MyAdvisoryOrderFinishEntranceCell.reuseIdentifier())
        
    }

    @IBOutlet weak var tableView: UITableView!
    
}

extension MyAdvisoryOrderFinishController: UITableViewDataSource, UITableViewDelegate {
    //MARK:---UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel?.numberOfSection ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: MyAdvisoryOrderFinishHeaderCell.reuseIdentifier(), for: indexPath) as! MyAdvisoryOrderFinishHeaderCell
            if let vm = viewModel {
                cell.updateData(vm: vm)
            }
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: MyAdvisoryOrderFinishDiagnoseCell.reuseIdentifier(), for: indexPath) as! MyAdvisoryOrderFinishDiagnoseCell
            if let vm = viewModel {
                cell.updateData(vm: vm)
            }
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: MyAdvisoryOrderFinishEntranceCell.reuseIdentifier(), for: indexPath) as! MyAdvisoryOrderFinishEntranceCell
            if let vm = viewModel {
                cell.updateData(vm: vm, indexPath: indexPath)
            }
            return cell
        }
    }
    
    //MARK:---UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0: return 116
        case 1: return UITableView.automaticDimension
        case 5: if let vm = viewModel{
            return vm.score > 0 ? 226 : 49
        }
            return 49
        
                    
        default: return 49
        }
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0
        }
        return 16
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let vm = viewModel else { return }
        
        switch indexPath.section {
        case 0:
            let vc = DoctorDetailController(with: vm.docId)
            navigationController?.pushViewController(vc, animated: true)
        case 2:
            if !vm.canChat {
                HUDTool.show(message: "无聊天记录")
                return
            }
            let vc = ChatViewController(with: vm.diagnoseId)
            vc.businessType = vm.businessType
            navigationController?.pushViewController(vc, animated: true)
        case 3:
            pushPrescriptionOrderVc()
        case 4:
            pushMedicalRecordVC()
        case 5:
            if vm.score > 0 { return }
            
            let starRatingBGView = YSGNurseAppointMentStarRatingBGView(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
             starRatingBGView.show()
             starRatingBGView.delegate = self
            
            
//            let comment = UINib.loadViewFrom(CommentView.self)
//            comment.frame = CGRect(x: 0, y: 0, width: screenW, height: screenH)
//            comment.delegate = self
//            comment.show()
        default:
            break
        }
    }
    

    
}

extension MyAdvisoryOrderFinishController {
    
    func evaluateAction(_ evaluateString : String,_ stars : String){
        if let vm = viewModel {
            evaluate(orderId: vm.orderId, score: stars ,comment: evaluateString)
        }
    }
    
//    func starRatingView(_ starRatingView: StarRatingView, count: Float) {
//        if let vm = viewModel {
//            evaluate(orderId: vm.orderId, score: count)
//        }
//    }
    
    private func pushPrescriptionOrderVc() {
        let orderVc = PrescriptionOrderController()
        navigationController?.pushViewController(orderVc, animated: true)
    }
    
    private func pushMedicalRecordVC() {
        guard let viewModel = viewModel else { return }
        
        let vc = MedicalRecordViewController(with: viewModel.diagnoseId)
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension MyAdvisoryOrderFinishController {
    func loadData() {
        HUDTool.showActivity()
        MyOrderViewModel.getAdvisoryOrderDetailFinish(diagnoseId: diagnoseId, success: { (model) in
            HUDTool.hideActivity()
            self.viewModel = MyAdvisoryOrderFinishViewModel(model: model)
        }) { (errorMsg) in
            HUDTool.hideActivity()
            HUDTool.showError(message: errorMsg)
        }
    }
    
    func evaluate(orderId: String, score: String,comment:String) {
        HUDTool.showActivity()
        MyOrderViewModel.getAdvisoryOrderEvaluate(orderId: orderId, score: score,comment: comment, success: { (msg) in
            HUDTool.hideActivity()
            self.closure?()
            self.loadData()
        }) { (errorMsg) in
            HUDTool.hideActivity()
            HUDTool.show(message: errorMsg)
        }
    }
}
